package com.hongyun.tms.web.controller;


import com.google.common.collect.Lists;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.Dept;
import com.hongyun.tms.common.annotation.Dict;
import com.hongyun.tms.common.annotation.Translate;
import com.hongyun.tms.common.constants.TranslateAnnotationConstant;
import com.hongyun.tms.domain.AjaxResult;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 测试@Dict注解是否能正确翻译字典to dictStr
 * </p>
 *
 * @author liPeng
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/test")
public class DemoController {

    /**
     * 测试分页返回时@Dict是否生效
     *
     * @return result
     */
    @GetMapping("pageDict")
    public AjaxResult<PageResponseDto<UserInfo>> pageDict() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(1798523472486367233L);
        userInfo.setAge(10);
        userInfo.setUsername("马斯克");
        userInfo.setCompany("2");
        userInfo.setSex(1);
        userInfo.setShipType("001");
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setAge(31);
        userInfo2.setUsername("张希悦");
        userInfo2.setCompany("1");
        userInfo2.setSex(0);
        userInfo2.setShipType("003");
        List<UserInfo> list = Lists.newArrayList();
        list.add(userInfo);
        list.add(userInfo2);
        PageResponseDto<UserInfo> pageResponseDto = new PageResponseDto<>();
        pageResponseDto.setTotal(2L);
        pageResponseDto.setPageSize(10L);
        pageResponseDto.setStartPage(1L);
        pageResponseDto.setRecords(list);
        return AjaxResult.success(pageResponseDto);
    }

    /**
     * 测试list返回时@Dict是否生效
     *
     * @return result
     */
    @GetMapping("listDict")
    public AjaxResult<List<UserInfo>> listDict() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(1798523472486367233L);
        userInfo.setAge(10);
        userInfo.setUsername("马斯克");
        userInfo.setCompany("2");
        userInfo.setSex(1);
        userInfo.setDeptId(1635579167369170959L);
        userInfo.setShipType("001");
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setAge(31);
        userInfo2.setUsername("张希悦");
        userInfo2.setCompany("1");
        userInfo2.setSex(0);
        userInfo2.setShipType("003");
        userInfo2.setDeptId(1788825762721861634L);
        List<UserInfo> list = Lists.newArrayList();
        list.add(userInfo);
        list.add(userInfo2);
        return AjaxResult.success(list);
    }

    /**
     * 只返回单个对象时
     *
     * @return
     */
    @GetMapping("dict")
    public AjaxResult<UserInfo> dict() {
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(10);
        userInfo.setUserId(1798523717429526530L);
        userInfo.setUsername("马斯克");
        userInfo.setCompany("2");
        userInfo.setSex(1);
        userInfo.setShipType("001");
        userInfo.setDeptId(1788834473909587969L);
        return AjaxResult.success(userInfo);
    }

    @Data
    static class UserInfo {
        private static final long serialVersionUID = 1L;
        private String username;

        @Translate(fieldName = "userNameStr", tableName = TranslateAnnotationConstant.TABLE_STAFF)
        private Long userId;
        private Integer age;

        @Dict(code = "GENDER", fieldName = "sexText")
        private Integer sex;

        @Dict(code = "COMPANY_STAFF")
        private String company;


        @Dict(code = "SHIP_KIND", fieldName = "shipStr")
        private String shipType;

        @Dept
        private Long deptId;
    }
}
